using System.IO;
using System.Reflection;
using System.Windows.Media.Imaging;
using System.ComponentModel;

namespace Microsoft.WPF.Samples
{
    /// <summary>
    /// A person to be visualized by the BizCardFlow.
    /// </summary>
    public class Person 
        : INotifyPropertyChanged
    {
        //Fields

        private string _firstName;
        private string _lastName;
        private string _postalCode;
        private string _address;
        private string _phone;
        private string _gender;
        private string _city;
        private string _email;
        private BitmapSource _bitmap;

        // Events

        #region PropertyChanged
        private static readonly object _propertyChangedLock = new object();
        private event PropertyChangedEventHandler _propertyChanged;

        public event PropertyChangedEventHandler PropertyChanged
        {
            add { lock (_propertyChangedLock) { _propertyChanged += value; }  }
            remove { lock (_propertyChangedLock) { _propertyChanged -= value; }  }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if(_propertyChanged==null) return;
            _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        // Properties

        #region FirstName
		/// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>The first name.</value>
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; OnPropertyChanged("FirstName");}
        } 
	    #endregion

        #region Fullname
		/// <summary>
        /// Gets the full name.
        /// </summary>
        /// <value>The first and last name combined.</value>
        public string Fullname
        {
            get { return string.Concat(FirstName, " ", LastName); }   
        }  
	    #endregion

        #region LastName
	    /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        /// <value>The last name.</value>
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; OnPropertyChanged("LastName"); }
        } 
	    #endregion

        #region PostalCode
	    /// <summary>
        /// Gets or sets the post code.
        /// </summary>
        /// <value>The post code.</value>
        public string PostalCode
        {
            get { return _postalCode; }
            set { _postalCode = value; OnPropertyChanged("PostalCode"); }
        } 
	    #endregion

        #region Address
        /// <summary>
        /// Gets or sets the adress.
        /// </summary>
        /// <value>The adress.</value>
        public string Address
        {
            get { return _address; }
            set { _address = value; OnPropertyChanged("Address"); }
        } 
	    #endregion

        #region Address
        /// <summary>
        /// Gets or sets the adress.
        /// </summary>
        /// <value>The adress.</value>
        public string FullAddress
        {
            get { return string.Concat(_address, "\n", _postalCode, " ", _city); }
        }
        #endregion

        #region Phone
		/// <summary>
        /// Gets or sets the phone.
        /// </summary>
        /// <value>The phone.</value>
        public string Phone
        {
            get { return _phone; }
            set { _phone = value; OnPropertyChanged("Phone");}
        } 
	    #endregion

        #region Gender
		/// <summary>
        /// Gets or sets the gender.
        /// </summary>
        /// <value>The gender.</value>
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; OnPropertyChanged("Gender");}
        } 
	    #endregion

        #region City
		/// <summary>
        /// Gets or sets the home town.
        /// </summary>
        /// <value>The home town.</value>
        public string City
        {
            get { return _city; }
            set { _city = value; OnPropertyChanged("City");}
        } 
	    #endregion

        #region Email
        /// <summary>
        /// Gets or sets the email.
        /// </summary>
        /// <value>The email.</value>
		public string Email
        {
            get { return _email; }
            set { _email = value; OnPropertyChanged("Email");}
        } 
	    #endregion

        #region Image
        /// <summary>
        /// Sets the image.
        /// </summary>
        /// <value>The image.</value>
        public string Image
        {
            set
            {
                //if (string.IsNullOrEmpty(value)) return;

                //var imageDir =
                //    Path.Combine(
                //        Path.GetDirectoryName(
                //            Assembly.GetExecutingAssembly().Location), 
                //        "images");

                //if (!Directory.Exists(imageDir)) return;

                //var fileName = Path.Combine(imageDir, value);
                //if (!File.Exists(fileName)) return;

               // _bitmap = BitmapFrame.Create(File.OpenRead(fileName));
                _bitmap = BitmapFrame.Create(File.OpenRead(value));
            }
        }

        #endregion

        #region Bitmap
        /// <summary>
        /// Gets or sets the bitmap.
        /// </summary>
        /// <value>The bitmap.</value>
        public BitmapSource Bitmap
        {
            get { return _bitmap; }
            set { _bitmap = value; OnPropertyChanged("Bitmap");}
        }
        #endregion
    }
}